using System;
using System.Web.UI;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Utility;

namespace PHSRAG.Insight21
{
    /// <summary>
    /// The ControlHost page is a generic host for any control (web custom control, user control, etc.). It provides the
    /// site within which contained control can operate.
    /// </summary>
    /// <remarks>
    /// <p>There are many cases when the need arises to open a new window on the client and display in that window certain
    /// content. The content may differ widely depending on the context. The idea is to invoke the ControlHost page and
    /// specify the path of a control to load. The ControlHost will simply load that control dynamically.</p>
    /// <p>When used in conjunction with client-side javascript, the ControlHost provides a simple mechanism of a pop-up
    /// window.</p>
    /// <p>The ControlHost uses two controls on itself; an annunciator and a place holder. The annunciator displays
    /// messages (as needed) and the place holder is where the ControlHost dynamically loads whatever control you ask
    /// it to load.</p>
    /// </remarks>
    /// <example>
    /// The following example shows how the ControlHost page would be used from the client-side.
    /// <code>
    /// <![CDATA[
    /// ...
    /// //Assume that openPopup is a button on some page.
    /// openPopup.Attributes["onclick"] = "window.open('/Insight21/Common/ControlHost.aspx?Load=/Insight21/Common/SearchSupport/Picker.ascx');"
    /// ...
    /// ]]>
    /// </code>
    /// </example>
    public partial class ControlHost : BasePage
    {
        #region Event Handlers

        /// <summary>
        /// Load the control specified in the query string and add it to the place holder.
        /// </summary>
        /// <param name="sender">Sender of the event</param>
        /// <param name="e">Arguments associated with the event</param>
        protected void OnPageLoad(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    Control c = LoadControl(Request.QueryString["Load"]);
                    c.ID = "dynamicallyLoadedControl";
                    placeHolder.Controls.Add(c);
                }
                catch (Exception ex)
                {
                    annunciator.Title = "Failed to load control";
                    annunciator.Message = ExceptionReport.GetExceptionInfo(ex, false);
                    annunciator.Visible = true;
                }
            }
        }

        #endregion

        #region Web Form Designer generated code

        /// <summary>
        /// Initialize the page.
        /// </summary>
        /// <param name="e">Argument associated with the event</param>
        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.OnPageLoad);
        }

        #endregion
    }
}